#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# Cloud.com Bridge setup script.
#

settingsFile="/etc/cloudstack/management/ec2-service.properties"

function readCurrentSettings () {
	readVar managementServer
	readVar cloudAPIPort
    readVar cloudstackVersion
	readVar WSDLVersion
	readVar keystore
	readVar keystorePass
	readVar m1.small.serviceId
 	readVar m1.large.serviceId
	readVar m1.xlarge.serviceId
	readVar c1.medium.serviceId
	readVar c1.xlarge.serviceId
	readVar m2.xlarge.serviceId
	readVar m2.2xlarge.serviceId
	readVar m2.4xlarge.serviceId
	readVar cc1.4xlarge.serviceId
	readVar dbHost
	readVar dbName
	readVar dbUser
	readVar dbPassword
}

function readVar () {
	local _pointer=$(sub "$1")
	local _tmp="`cat $settingsFile|grep \"$1=\"|awk -F= '{print $2}'|tr -d '\r'`"
	eval $_pointer="$_tmp"
}

function readValue () {
	local _value
	local _tmp
	local _pointer=$(sub "$2")
	eval _value="\$$_pointer"
	printf "%s [%s]: " "$1" "$_value"
	read -e _tmp

	if [ "$_tmp" != "" ]
	then
		eval $_pointer="$_tmp"
	fi
}

function getNewValues () {
	readValue "Management server hostname or IP" managementServer
	readValue "Management server port" cloudAPIPort 

	readValue "Service offering ID for m1.small" m1.small.serviceId
	readValue "Service offering ID for m1.large" m1.large.serviceId
	readValue "Service offering ID for m1.xlarge" m1.xlarge.serviceId

	readValue "Service offering ID for c1.medium" c1.medium.serviceId
	readValue "Service offering ID for c1.xlarge" c1.xlarge.serviceId

	readValue "Service offering ID for m2.xlarge" m2.xlarge.serviceId
	readValue "Service offering ID for m2.2xlarge" m2.2xlarge.serviceId
	readValue "Service offering ID for m2.4xlarge" m2.4xlarge.serviceId
	readValue "Service offering ID for cc1.4xlarge" cc1.4xlarge.serviceId
}

function sub () {
	echo "$1" | awk '{gsub(/\./, "_", $0); print $0}'	
}

function saveValue () {
	local _pointer=$(sub "$1")
	local _value
	eval _value="\$$_pointer"
	echo "$1=$_value" >> $settingsFile
}

function saveValues () {
	cat /dev/null > $settingsFile
	saveValue managementServer
	saveValue cloudAPIPort
	saveValue cloudstackVersion
	saveValue WSDLVersion
	saveValue keystore
	saveValue keystorePass
	saveValue m1.small.serviceId
 	saveValue m1.large.serviceId
	saveValue m1.xlarge.serviceId
	saveValue c1.medium.serviceId
	saveValue c1.xlarge.serviceId
	saveValue m2.xlarge.serviceId
	saveValue m2.2xlarge.serviceId
	saveValue m2.4xlarge.serviceId
	saveValue cc1.4xlarge.serviceId
	saveValue dbHost
	saveValue dbName
	saveValue dbUser
	saveValue dbPassword
}

echo "Welcome to the CloudBridge setup."


if [ ! -e $settingsFile ]
then 
    settingsFile=$1
    if [ ! -e $settingsFile ]
    then
	echo "Error: Settings file not found."
	exit 1
    fi
fi
if [ ! -w $settingsFile ]
then 
	echo "Error: Insufficient permissions. Run as super-user."
	exit 1
fi

readCurrentSettings

printf "Enter suitable values or press enter for default. \n\n"

getNewValues
saveValues

printf "\nValues saved. Restart the cloud-bridge service for the changes to become active.\n\n"
